package service

import (
	awesomeTask "awesomeTask/commom"
	"awesomeTask/config"
	"awesomeTask/system/log"
	"awesomeTask/system/resource"
	"regexp"
)

type DatabaseService struct {
}

func (ioc DatabaseService) Register() {
	database := config.GetConfigByKey("database.master.database").(string)
	createDatabase := "CREATE DATABASE IF NOT EXISTS " + database + ";"
	_, err := awesomeTask.GetConnectionByDatabaseName("mysql").Exec(createDatabase)
	if err != nil {
		log.GetLogger().Error(err.Error())
		panic(err)
	}
	sql, err := resource.GetResourceManager().FindString("doc/excute.sql")
	if err != nil {
		panic(err)
	}
	//fmt.Println(sql)
	createRex, err := regexp.Compile(`CREATE([\s\S]*?);`)
	if err != nil {
		panic(err)
	}
	createSql := createRex.FindAll([]byte(sql), -1)
	for _, item := range createSql {
		_, err = awesomeTask.Execute(string(item))
		if err != nil {
			log.GetLogger().Error(err.Error())
			panic(err)
		}
	}

}
func (ioc DatabaseService) Boot() {

}
